<?php

class Wallet {
	private $id = NULL;
	private $info;
	private $amount;
	private $added_date;
	private $added_ip;
	
	public function getInfo() {return $this->info;}
	public function getAmount() {return $this->amount;}
	public function getAddedDate() {return date('d-m-Y H:i:s', $this->added_date);}
	public function getAddedIp() {return $this->added_ip;}
	
	public function setInfo($info) {
		$this->info = htmlentities($info);
	}
	
	public function setAmount($amount) {
		$this->amount = intval(floatval($amount) * 100) / 100;
	}
	
	public function __construct($row = null) {
		if (is_array($row)) {
			// wywolanie na podstawie podania wszystkich danych
			if (isset($row['id'])) $this->id = $row['id'];
			$this->info = $row['info'];
			$this->amount = $row['amount'];
			$this->added_date = $row['added_date'];
			$this->added_ip = $row['added_ip'];
			
		}
	}
	private function _getThisAsArray() {
		return array(
			'id' => $this->id,
			'info' => $this->info,
			'amount' => $this->amount,
			'added_date' => $this->added_date,
			'added_ip' => $this->added_ip,
		);
	}
	
	public function save() {
		if ($this->id != NULL) {
			// update
			WalletDAO::update($this->id, $this->_getThisAsArray() );
			return $this->id;
			
		} else {
			// insert
			return WalletDAO::insert( $this->_getThisAsArray() );
			
		}
	}
	
}

class WalletDAO {
	public static function getAll() {
		$rows = array();
		$sql = 'SELECT * FROM wallet ORDER BY added_date ASC';
		$res = mysql_query($sql);
		while ($row = mysql_fetch_assoc($res)) $rows[] = new Wallet($row);
		return $rows;
	}
	
	public static function update($id, $row) {
		unset ($row['id']); unset ($row['added_date']); unset ($row['added_ip']);  // tego przeciez nie uaktualniamy
		$id = intval($id);
		$sql = 'UPDATE wallet SET ';
		foreach ($row as $k => $v) $sql .= $k .'="'. mysql_real_escape_string($v) .'"';
		try {
			mysql_query($sql);
		} catch (Exception $e) {
			die('Nie można było zmodyfikować portfela (' . $e->getMessage(). ')');
		}
	}
	
	public static function insert($row) {
		unset ($row['id']);
		$row['added_date'] = time();
		$row['added_ip'] = $_SERVER['REMOTE_ADDR'];
		$sql = 'INSERT INTO wallet (';
		$vals = '';
		$is_first = true;
		foreach ($row as $k => $v) {
			if ($k == 'id') continue;
			if (!$is_first) { $sql.=','; $vals.=','; }
			if ($is_first) $is_first = false;
			$sql.= $k;
			$vals.= '"'. mysql_real_escape_string($v) . '"';
		}
		$sql .=') VALUES (' . $vals . ')';
		/*echo '<pre>';
		print_r($sql);
		echo '</pre>';
		exit();*/
		unset ($vals);
		try {
			$res = mysql_query($sql);
			return mysql_insert_id();
		} catch (Exception $e) {
			die('Nie można było dodać wpisu w portfelu (' . $e->getMessage() .')');
		}
	}
	
	public static function getWalletSum() {
		$sql = 'SELECT SUM(amount) AS suma FROM wallet';
		try {
			$res = mysql_query($sql);
			$row = mysql_fetch_assoc($res);
		} catch (Exception $e) {
			die('Nie można było wybrać sumy portfela (' . $e->getMessage() .')');
		}
		return $row['suma'];
	}

}